var tellPhone = "";
function findUserByTellPhone() {
    let tellphone = document.getElementById("telnumber");
    let name = document.getElementById("name");
    let kind = document.getElementById("kind");
    let Balance = document.getElementById("Balance");   //
    let cutNum = document.getElementById("cutNum");
    let perNum = document.getElementById("perNum");
    let nuseNum = document.getElementById("nuseNum");
    let rootNum = document.getElementById("rootNum");
    let dyeNum = document.getElementById("dyeNum");
    let washNum = document.getElementById("washNum");
    let blackNum = document.getElementById("blackNum");

    let inMoney = document.getElementById("inMoney");
    let inNum = document.getElementById("inNum");
    let inNum2 = document.getElementById("inNum2");
    let inNum3 = document.getElementById("inNum3");
    let inNum4 = document.getElementById("inNum4");
    let inNum5 = document.getElementById("inNum5");
    let inNum6 = document.getElementById("inNum6");
    let inNum7 = document.getElementById("inNum7");
    let inNum8 = document.getElementById("inNum8");

    let invest = document.getElementById("invest");
    let invest1 = document.getElementById("invest1");
    let invest2 = document.getElementById("invest2");
    let invest3 = document.getElementById("invest3");
    let invest4 = document.getElementById("invest4");
    let invest5 = document.getElementById("invest5");
    let invest6 = document.getElementById("invest6");
    let invest7 = document.getElementById("invest7");
    let cost = document.getElementById("cost");
    //手机号验证
    const reg1 =/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
    if (!reg1.test(tellphone.value)) {
        name.value = "";
        kind.value = "";
        Balance.value = "";
        cutNum.value = "";
        perNum.value = "";
        nuseNum.value = "";
        rootNum.value = "";
        dyeNum.value = "";
        washNum.value = "";
        blackNum.value = "";

        invest.disabled = true;
        invest1.disabled = true;
        invest2.disabled = true;
        invest3.disabled = true;
        invest4.disabled = true;
        invest5.disabled = true;
        invest6.disabled = true;
        invest7.disabled = true;
        cost.disabled = true;
        return false;
    }
    else {
        $.ajax({
          url:"http://localhost:8080/Barbr/dispose?action=search&tellphone="+tellphone.value,
          type:"get",
          cache:false,
          data:{},
          dataType:"json",
          success(data){
              if (data) {
                  var user = data;
                  console.log(user);
                  tellPhone = user.tellphone;
                  name.value = user.username;
                  var type = user.type;
                  switch(type){
                      case '0':kind.value = "剪发";break;
                      case '1':kind.value = "洗发";break;
                      case '2':kind.value = "护理";break;
                      case '3':kind.value = "补发根";break;
                      case '4':kind.value = "染发";break;
                      case '5':kind.value = "黑油";break;
                      case '6':kind.value = "烫发+剪发";break;
                      case '7':kind.value = "烫发+染发";break;
                      case '8':kind.value = "洗发+染发";break;
                      case '9':kind.value = "剪发+黑油";break;
                      case '10':kind.value = "剪发+染发";break;
                  }
                  Balance.value = user.invest;
                  cutNum.value = user.haircut;
                  perNum.value = user.perm;
                  nuseNum.value = user.wash_protect;
                  rootNum.value = user.hairroot;
                  dyeNum.value = user.colour;
                  washNum.value = user.wash;
                  blackNum.value = user.black;

                  invest.disabled = false;
                  invest1.disabled = false;
                  invest2.disabled = false;
                  invest3.disabled = false;
                  invest4.disabled = false;
                  invest5.disabled = false;
                  invest6.disabled = false;
                  invest7.disabled = false;
                  cost.disabled = false;

              } else {
                  name.value = "";
                  kind.value = "";
                  Balance.value = "";
                  cutNum.value = "";
                  perNum.value = "";
                  nuseNum.value = "";
                  rootNum.value = "";
                  dyeNum.value = "";
                  washNum.value = "";
                  blackNum.value = "";

                  invest.disabled = true;
                  invest1.disabled = true;
                  invest2.disabled = true;
                  invest3.disabled = true;
                  invest4.disabled = true;
                  invest5.disabled = true;
                  invest6.disabled = true;
                  invest7.disabled = true;
                  cost.disabled = true;
                  // alert(res.data.msg);
              }
          },
          error(){
              alert("请求失败")
          }
        });
    }
}
function investTimes(obj) {
    let cutNum = document.getElementById("cutNum");
    let perNum = document.getElementById("perNum");
    let nuseNum = document.getElementById("nuseNum");
    let rootNum = document.getElementById("rootNum");
    let dyeNum = document.getElementById("dyeNum");
    let washNum = document.getElementById("washNum");
    let blackNum = document.getElementById("blackNum");

    let inNum = document.getElementById("inNum");
    let inNum2 = document.getElementById("inNum2");
    let inNum3 = document.getElementById("inNum3");
    let inNum4 = document.getElementById("inNum4");
    let inNum5 = document.getElementById("inNum5");
    let inNum6 = document.getElementById("inNum6");
    let inNum7 = document.getElementById("inNum7");
    if(obj === '剪发' && inNum.value === '') return false;
    if(obj === '烫发' && inNum2.value === '') return false;
    if(obj === '护理' && inNum3.value === '') return false;
    if(obj === '补发根' && inNum4.value === '') return false;
    if(obj === '染发' && inNum5.value === '') return false;
    if(obj === '洗发' && inNum6.value === '') return false;
    if(obj === '黑油' && inNum7.value === '') return false;

    var flag = confirm("确定充值"+obj+"次数？");
    if(flag){
        if(obj === '剪发'){
            cutNum.value = cutNum.value*1 + inNum.value*1;
            inNum.value = "";
        }
        if(obj === '烫发'){
            perNum.value = perNum.value*1 + inNum2.value*1;
            inNum2.value = "";
        }
        if(obj === '护理'){
            nuseNum.value = nuseNum.value*1 + inNum3.value*1;
            inNum3.value = "";
        }
        if(obj === '补发根'){
            rootNum.value = rootNum.value*1 + inNum4.value*1;
            inNum4.value = "";
        }
        if(obj === '染发'){
            dyeNum.value = dyeNum.value*1 + inNum5.value*1;
            inNum5.value = "";
        }
        if(obj === '洗发'){
            washNum.value = washNum.value*1 + inNum6.value*1;
            inNum6.value = "";
        }
        if(obj === '黑油'){
            blackNum.value = blackNum.value*1 + inNum7.value*1;
            inNum7.value = "";
        }
    }

}
function investCard() {
    let Balance = document.getElementById("Balance");       /*已有金额*/
    let inMoney = document.getElementById("inMoney");       /*充值金额*/
    let telnumber = document.getElementById("telnumber");   /*充值人电话*/
    if(inMoney.value === ""){
        return false;
    }else{
        var flag = confirm("确定充值？");
        if(flag) {
            $.ajax({
               url:"http://localhost:8080/Barbr/invest?action=investCard&investCard="+inMoney.value+"&tellphone="+telnumber.value,
               type: "get",
               cache: false,
               data:{},
               success(data) {
                   Balance.value = Balance.value * 1.0 + inMoney.value * 1.0;
                   inMoney.value = "";
                   alert(data);
               },
                error() {
                    inMoney.value = "";
                    alert("请求失败！");
                }
            });
           /* axios.post("http://localhost:8081/user/investCard", {
                investCard: inMoney.value,
                tellphone: telnumber.value
            }).then(res => {
                if (res.data.state) {
                    Balance.value = Balance.value * 1.00 + inMoney.value * 1.00;
                    inMoney.value = "";
                    alert(res.data.msg);
                } else {
                    inMoney.value = "";
                    alert(res.data.msg);
                }
            })*/
        }
    }
}
function cost() {
    //var inNum8 = document.getElementById("inNum8");     //消费金额  消费按钮左边的输入框
    let user = {
        "cutNum":"",
        "perNum":"",
        "nuseNum":"",
        "rootNum":"",
        "dyeNum":"",
        "washNum":"",
        "blackNum":"",
        "tellphone":"",
        "totalcost":""
    };
   // if(inNum8.value === "")return false;        /*如果不填入消费金额无法消费*/
    var flag = confirm("请核对上面信息，确定消费？");
    if(flag){
        //获得已充值的次数
        user.cutNum = document.getElementById("cutNum").value;
        user.perNum = document.getElementById("perNum").value;
        user.nuseNum = document.getElementById("nuseNum").value;
        user.rootNum = document.getElementById("rootNum").value;
        user.dyeNum = document.getElementById("dyeNum").value;
        user.washNum = document.getElementById("washNum").value;
        user.blackNum = document.getElementById("blackNum").value;
        user.tellphone = tellPhone;
        user.totalcost = 0;
        $.ajax({
           url:"http://localhost:8080/Barbr/investMoney",
           type:"post",
           data:JSON.stringify(user),
           cache:false,
           success(data){
               alert("充值成功!");
           },
           error(){
               alert("修改err");
           }
        });
    }
}
